<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\RedAccessModel;

class RedAccess extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取添加权限二维码
     * @return mixed
     */
    public function getAddQrcode()
    {
        $_url = murl('entry//scan',array('m'=>KUYUAN_NAME,"scan_type"=>"red_access"),true,true);
        return $this->getQrcode("red_access_qrcode","red_access_qrcode",$_url);
    }

    /**
     * 获取指定字段值
     * @param string $_field 字段
     * @return mixed
     */
    public function value($_field)
    {
        return RedAccessModel::where("uid",$this->_uid)
            ->where("uniacid",$this->_uniacid)
            ->value($_field);
    }

    /**
     * 新增发红包权限
     */
    public function add()
    {
        return RedAccessModel::create(array(
            "uniacid" => $this->_uniacid,
            "uid" => $this->_uid
        ));
    }

    /**
     * 修改一个权限
     * @param $_data
     * @param int $_uid
     * @return mixed
     */
    public function edit($_data,$_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        return RedAccessModel::where("uid",$_uid)
            ->where("uniacid",$this->_uniacid)
            ->update($_data);
    }

    /**
     * 获取列表
     */
    public function getList()
    {
        return RedAccessModel::where("uniacid",$this->_uniacid)
            ->where("status","!=",9)
            ->field(array("id","uid","status"))
            ->order("time","desc")
            ->cache(!KUYUAN_DEBUG)
            ->select();
    }
}